var local="http://47.104.10.151";
//无参数GET请求
var xmlHttp;
//通过鼠标点击事件创建HTTP请求
function onClickAjax() {
    xmlHttp = createXMLHttp();
    var name = document.getElementById("user_name").value;
    var email = document.getElementById("email").value;
    var mobile = document.getElementById("mobile").value;
    var password = document.getElementById("password").value;
    var nick = document.getElementById("nick").value;
    var type = document.getElementById("type").value;
    var invite_code=document.getElementById("invate_code").value;


    if(invite_code==""){
        if (password.length == 8) {
            txt = "name=" + name + "&email=" + email + "&mobile=" + mobile + "&password=" + password + "&nick=" + nick + "&user_type=" + type;
            var url = local+":8080/login/addUser";//服务器URL
            xmlHttp.open("POST", url, true);
            xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            xmlHttp.send(txt);
            //传输结束
            alert("注册成功");
            window.open('../index.html');
        }else{
            alert("请输入八位密码！");
            // 47.104.10.151
        }
    } else {
        if (password.length == 8) {
            alert(password);
            url1 = "http://47.104.10.151:8080/login/inviteCode";
            txt1 = "invite_code=" + invite_code;
            xmlHttp.open("POST", url1, true);
            xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            xmlHttp.send(txt1);
            xmlHttp.onreadystatechange = function () {
                if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
                    var result = xmlHttp.responseText;
                    var json = eval("(" + result + ")");
                    var error = "error";

                    if (json.msg == error) {
                        alert("邀请码错误");
                    }
                    else {
                        txt = "name=" + name + "&email=" + email + "&mobile=" + mobile + "&password=" + password + "&nick=" + nick + "&user_type=" + type ;
                        var url = local+":8080/login/addUser";//服务器URL
                        xmlHttp.open("POST", url, true);
                        xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                        xmlHttp.send(txt);
                        //传输结束

                    }
                }
            };
            alert("注册成功");
            window.open('../index.html');
        }else{
            alert("请输入八位密码！");
        }
    }
}

//适配浏览器
//根据不同浏览器发送不同请求
function createXMLHttp() {
    //使用大部分浏览器
    var xmlHttp;
    if(window.XMLHttpRequest){
        xmlHttp=new XMLHttpRequest();
    }
    if(window.ActiveXObject){
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        if(!xmlHttp){
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
    }
    return xmlHttp;
}

function changeUserName(){
    xmlHttp1=createXMLHttp();
    var user_name=document.getElementById("user_name").value;
    url1 = local+":8080/login/searchUserName";
    txt1 = "user_name=" + user_name;
    xmlHttp1.open("POST", url1, true);
    xmlHttp1.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp1.send(txt1);
    xmlHttp1.onreadystatechange = function () {
        if (xmlHttp1.readyState == 4 && xmlHttp1.status == 200) {
            var result = xmlHttp1.responseText;
            var json = eval("(" + result + ")");
            var error = "error";
            if (json.msg == error) {
                document.getElementById('user_name1').innerHTML="(未注册)";
                document.getElementById('user_name1').style.color="#00EE00";
            }
            else{
                document.getElementById('user_name1').innerHTML="(已注册)";
                document.getElementById('user_name1').style.color="#FF0000";
            }
        }
    };

}

function changeMobile(){
    xmlHttp1=createXMLHttp();
    var mobile=document.getElementById("mobile").value;
    url1 = local+":8080/login/searchMobile";
    txt1 = "mobile=" + mobile;
    xmlHttp1.open("POST", url1, true);
    xmlHttp1.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp1.send(txt1);
    xmlHttp1.onreadystatechange = function () {
        if (xmlHttp1.readyState == 4 && xmlHttp1.status == 200) {
            var result = xmlHttp1.responseText;
            var json = eval("(" + result + ")");
            var error = "error";
            if (json.msg == error) {
                document.getElementById('mobile1').innerHTML="(未注册)";
                document.getElementById('mobile1').style.color="#00EE00";
            }
            else{
                document.getElementById('mobile1').innerHTML="(已注册)";
                document.getElementById('mobile1').style.color="#FF0000";
            }
        }
    };
}

function changeEmail(){


    xmlHttp1=createXMLHttp();
    var email=document.getElementById("email").value;
    url1 = local+":8080/login/searchEmail";
    txt1 = "email=" + email;
    xmlHttp1.open("POST", url1, true);
    xmlHttp1.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp1.send(txt1);
    xmlHttp1.onreadystatechange = function () {
        if (xmlHttp1.readyState == 4 && xmlHttp1.status == 200) {
            var result = xmlHttp1.responseText;
            var json = eval("(" + result + ")");
            var error = "error";
            if (json.msg == error) {
                document.getElementById('email1').innerHTML="(未注册)";
                document.getElementById('email1').style.color="#00EE00";
            }
            else{
                document.getElementById('email1').innerHTML="(已注册)";
                document.getElementById('email1').style.color="#FF0000";
            }
        }
    };

}